/*
 * utils.c
 *
 *  Created on: 2022年2月4日
 *      Author: TamGitsun
 */


#include "utils.h"
#include <time.h>


k_tick_t get_sys_time(void)
{
	return tos_systick_get();
}

void set_sys_time(k_tick_t t)
{
	tos_systick_set(t);
}

void set_time(T_TIME * t)
{
	struct tm fmt;
	fmt.tm_year = t->year - 1900;
	fmt.tm_mon = t->month - 1;
	fmt.tm_mday = t->day;
	fmt.tm_hour = t->hour;
	fmt.tm_min = t->minute;
	fmt.tm_sec = t->second;
	time_t timer = mktime(&fmt);
	tos_systick_set(timer * 1000);
}

T_TIME get_time(k_tick_t * t)
{
	T_TIME ret = {0};
	struct tm * fmt;
	time_t timer;
	if (NULL == t)
	{
		timer = tos_systick_get() / 1000;
	} else
	{
		timer = (*t) / 1000;
	}
	fmt = localtime(&timer);
	ret.year   = fmt->tm_year + 1900;
	ret.month  = fmt->tm_mon + 1;
	ret.day    = fmt->tm_mday;
	ret.hour   = fmt->tm_hour;
	ret.minute = fmt->tm_min;
	ret.second = fmt->tm_sec;
	return ret;
}

void get_format_time(char * buffer, size_t size)
{
	T_TIME t = get_time(NULL);
	snprintf(buffer, size, "%04d-%02d-%02d %02d:%02d:%02d",
			t.year, t.month, t.day, t.hour, t.minute, t.second);
}

void get_format_time_of_day(char * buffer, size_t size)
{
	T_TIME t = get_time(NULL);
	snprintf(buffer, size, "%02d:%02d:%02d", t.hour, t.minute, t.second);
}

void get_format_date(char * buffer, size_t size)
{
	T_TIME t = get_time(NULL);
	snprintf(buffer, size, "%04d-%02d-%02d", t.year, t.month, t.day);
}

void k_tick_t_2_format_time(k_tick_t t, char * buffer, size_t size)
{
	T_TIME t_ = get_time(&t);
	snprintf(buffer, size, "%04d-%02d-%02d %02d:%02d:%02d",
			t_.year, t_.month, t_.day, t_.hour, t_.minute, t_.second);
}

void k_tick_t_2_format_time_of_day(k_tick_t t, char * buffer, size_t size)
{
	T_TIME t_ = get_time(&t);
	snprintf(buffer, size, "%02d:%02d:%02d", t_.hour, t_.minute, t_.second);
}

void k_tick_t_2_format_date(k_tick_t t, char * buffer, size_t size)
{
	T_TIME t_ = get_time(&t);
	snprintf(buffer, size, "%04d-%02d-%02d", t_.year, t_.month, t_.day);
}

int date_cmp(T_TIME t1, T_TIME t2)
{
	if (t1.year != t2.year)
	{
		return t1.year - t2.year;
	}
	if (t1.month != t2.month)
	{
		return t1.month - t2.month;
	}
	if (t1.day != t2.day)
	{
		return t1.day - t2.day;
	}
	if (t1.hour != t2.hour)
	{
		return t1.hour - t2.hour;
	}
	if (t1.minute != t2.minute)
	{
		return t1.minute - t2.minute;
	}
	if (t1.second != t2.second)
	{
		return t1.second - t2.second;
	}
	return 0;
}

int date_diff(T_TIME t1, T_TIME t2, int diff_level)
{
	if ((diff_level & DIFF_LEVEL_YEAR) > 0)
	{
		if (t1.year != t2.year)
		{
			return t1.year - t2.year;
		}
	}
	if ((diff_level & DIFF_LEVEL_MONTH) > 0)
	{
		if (t1.month != t2.month)
		{
			return t1.month - t2.month;
		}
	}
	if ((diff_level & DIFF_LEVEL_DAY) > 0)
	{
		if (t1.day != t2.day)
		{
			return t1.day - t2.day;
		}
	}
	if ((diff_level & DIFF_LEVEL_HOUR) > 0)
	{
		if (t1.hour != t2.hour)
		{
			return t1.hour - t2.hour;
		}
	}
	if ((diff_level & DIFF_LEVEL_MINUTE) > 0)
	{
		if (t1.minute != t2.minute)
		{
			return t1.minute - t2.minute;
		}
	}
	if ((diff_level & DIFF_LEVEL_SECOND) > 0)
	{
		if (t1.second != t2.second)
		{
			return t1.second - t2.second;
		}
	}
	return 0;
}

day_t k_tick_t_2_day_t(k_tick_t t)
{
	return t / 86400000;  // t / (1000 * 24 * 60 * 60)
}

k_tick_t day_t_2_k_tick_t(day_t day)
{
	return ((k_tick_t) day) * 86400000;  // t * (1000 * 24 * 60 * 60)
}

int k_tick_t_2_day_seconds(k_tick_t t)
{
	return (t / 1000) % 86400;  // (t / 1000) % (24 * 60 * 60)
}

k_tick_t str_2_k_tick_t(const char * str)
{
	k_tick_t t = 0;
	for (uint32_t i = 0; i < strlen(str); ++i)
	{
		t = t * 10 + (str[i] - '0');
	}
	return t;
}

#include "fsl_debug_console.h"

void k_tick_t_2_str(k_tick_t t, char * buf, size_t size)
{
	if (0 == t)
	{
		snprintf(buf, size, "0");
		return;
	}
	memset(buf, 0, size);
	size_t len = 0;
	k_tick_t t_ = t;
	while (t_ > 0)
	{
		t_ /= 10;
		++len;
	}
	if (len >= size)
	{
		size_t n_skips = len - (size - 1);
		while (t > 0)
		{
			if (n_skips-- > 0)
			{
				t /= 10;
				continue;
			}
			buf[len-- - 1] = t % 10 + '0';
			t /= 10;
		}
		len = size - 1;
	} else
	{
		while (t > 0)
		{
			buf[len-- - 1] = t % 10 + '0';
			t /= 10;
		}
	}
}


static int random_int_next(random_int_generator_t * generator)
{
	int rc = rand();
	return (rc % (generator->range_end - generator->range_begin + 1)) + generator->range_begin;
}

void random_int_generator(random_int_generator_t * generator, int range_begin, int range_end)
{
	generator->seed = (uint32_t) get_sys_time();
	generator->range_begin = range_begin;
	generator->range_end = range_end;
	generator->next = random_int_next;
	srand(generator->seed);
}
